package com.xx.servlet;

import com.xx.bean.User;
import com.xx.service.UserService;
import com.xx.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

public class RegisterServlet extends HttpServlet {

    private UserServiceImpl userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        User user = new User(null,username,password,email);

        try{
            userService.doRegister(user);
            resp.sendRedirect(req.getContextPath()+"/pages/user/regist_success.html");
        }catch (Exception e){
            resp.getWriter().write("注册失败,用户名已存在");
        }

    }
}
